# encoding: utf-8
require 'sequel'

module Capevaluation
  module Service
    class WbaiFootballMatch < CaiQiu::Core::ServiceBase
      class << self

        # 获取数据库连接(框架添加事务使用<ServiceBase.method_missing>)
        def get_conn
          unless defined? @conn
            @conn = CaiQiu::Core::ServiceConn.DB_WBAI
          end
          @conn
        end


        #判断比赛是否已经开始
        #返回:
        # 开始-true
        # 未开始-false
        def is_match_begin_playing(match_id)
          info = Capevaluation::Dao::WbaiFootballMatch.where(:match_id=>match_id).select(:match_status).first
          return true if info.nil?
          info[:match_status].to_i.zero? ? false : true
        end

        def get_match_score(match_id_list)
          sql = "select match_id, host_score, away_score, host_half_score, away_half_score
                 from 500w.football_match where host_score is not null and away_score is not null
                 and match_id='#{match_id_list}' and match_status=4 "
          rows = self.get_conn[sql]
          ret_list = []
          rows.each do |row|
            ret_list.push(row.to_hash)
          end
          return ret_list
        end


        #
        # def get_match_score(match_id_list)
        #   #rows = Prediction::Dao::WbaiFootballMatch.where(:match_id=>match_id_list).where('host_score is not null').where('away_score is not null').select(:match_id, :host_score, :away_score, :host_half_score, :away_half_score)
        #
        #   sql = "select match_id, host_score, away_score, host_half_score, away_half_score
        #          from 500w.football_match where host_score is not null and away_score is not null
        #          and match_id='#{match_id_list}' and (substring(timediff (now(),(SELECT match_time
        #          FROM 500w.football_match where match_id='#{match_id_list}')),1,2)+'0')>4 "
        #   rows = self.get_conn[sql]
        #   ret_list = []
        #   rows.each do |row|
        #     ret_list.push(row.to_hash)
        #   end
        #   return ret_list
        # end
        #
        #
        # def get_match_team_info(match_id_list)
        #   rows = Prediction::Dao::WbaiFootballMatch.select(:match_id,:season_id,:host_id,:away_id).where(:match_id=>match_id_list)
        #   ret_list = []
        #   rows.each do |row|
        #     ret_list.push(row.to_hash)
        #   end
        #   return ret_list
        # end
        #
        # # 获取博彩公司信息
        # def get_company_info(company_id)
        #   result = Prediction::Dao::WbaiCompany.select(:company_id,:company_name_pre).where(:company_id=>company_id).first
        #   if !result.nil?
        #     result = result.to_hash
        #   else
        #     result = {}
        #   end
        #   return result
        # end
        #
        # #获取球队信息
        # def get_team_info(team_id)
        #   result = Prediction::Dao::WbaiFootballTeam.select(:team_id,:team_name_pre).where(:team_id=>team_id).first
        #   if !result.nil?
        #     result = result.to_hash
        #   else
        #     result = {}
        #   end
        #   return result
        # end

      end
    end
  end
end
